package graphic;

import graphic.mycomponents.SingleChatPanel;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JTabbedPane;
import javax.swing.plaf.basic.BasicButtonUI;

public class ChatDialogPanel extends JDialog implements KeyListener
{
	protected static ChatDialogPanel CHATDIALOGPANEL; 
	
	JTabbedPane tabbedPane;
	
	HashMap<String, SingleChatPanel> chats;
	
	public ChatDialogPanel()
	{
		super(MainFrame.getInstance());
		super.setTitle("Chat Area");
		
		this.tabbedPane = new JTabbedPane();
        this.tabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
		this.tabbedPane.addKeyListener(this);
		
		this.chats = new HashMap<String, SingleChatPanel>();
				
		super.getContentPane().add(this.tabbedPane);
		super.setPreferredSize(new Dimension(200, 200));
		
		int x = ((int)super.getParent().getSize().getWidth() - (int)super.getSize().getWidth()) / 2;
		int y = ((int)super.getParent().getSize().getHeight() - (int)super.getSize().getHeight()) / 2;
		super.setLocation(x, y);
		
		super.addKeyListener(this);
		super.validate();
		super.pack();
		super.setModal(false);
	}
		
	public void openChat(String userId)
	{
		SingleChatPanel chat = this.chats.get(userId);

		if(chat == null)
		{
			chat = new SingleChatPanel(userId);
			this.chats.put(userId, chat);
			this.tabbedPane.addTab(userId, chat);
						
			System.out.println("la tab per chattare con " + userId + " non esiste e la creo");
			
		} else
			this.tabbedPane.setSelectedComponent(chat);
		
		super.setVisible(true);
	}
	
	public void closeChat(String userId)
	{
		if(userId != null)
		{
			SingleChatPanel chat = this.chats.get(userId);
			
			this.chats.remove(userId);
			this.tabbedPane.remove(chat);
			
			if(chats.isEmpty())
				super.setVisible(false);
		}
		else
			super.setVisible(false);
	}
	
	public void writeChatMessage(String receiver, String writer, String chatMessage)
	{
		this.openChat(receiver);
		this.chats.get(receiver).writeChatMessage(writer, chatMessage);
	}
	
	public HashMap<String, SingleChatPanel> getChats() {
		return chats;
	}

    private class TabButton extends JButton implements ActionListener {
        public TabButton() {
            int size = 17;
            setPreferredSize(new Dimension(size, size));
            setToolTipText("close this tab");
            //Make the button looks the same for all Laf's
            setUI(new BasicButtonUI());
            //Make it transparent
            setContentAreaFilled(false);
            //No need to be focusable
            setFocusable(false);
            setBorder(BorderFactory.createEtchedBorder());
            setBorderPainted(false);
            //Making nice rollover effect
            //we use the same listener for all buttons
            addMouseListener(buttonMouseListener);
            setRolloverEnabled(true);
            //Close the proper tab by clicking the button
            addActionListener(this);
        }

		@Override
		public void actionPerformed(ActionEvent arg0) {}
    }
    
    private final static MouseListener buttonMouseListener = new MouseAdapter() {
        public void mouseEntered(MouseEvent e) {
            Component component = e.getComponent();
            if (component instanceof AbstractButton) {
                AbstractButton button = (AbstractButton) component;
                button.setBorderPainted(true);
            }
        }

        public void mouseExited(MouseEvent e) {
            Component component = e.getComponent();
            if (component instanceof AbstractButton) {
                AbstractButton button = (AbstractButton) component;
                button.setBorderPainted(false);
            }
        }
    };

	
	public static ChatDialogPanel getIstance()
	{
		if(CHATDIALOGPANEL == null) CHATDIALOGPANEL = new ChatDialogPanel();		
		return CHATDIALOGPANEL;
	}

	@Override
	public void keyPressed(KeyEvent e) {}

	@Override
	public void keyReleased(KeyEvent e) {}

	@Override
	public void keyTyped(KeyEvent e) { this.closeChat(null); }
}